<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Resources\BookCommentsResource;
use App\Models\BookComments;
use App\Models\BookCommentsStart;
use App\Models\Books;
use App\Models\BookScore;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/***
 * 书籍评论
 * Class BookCommentsController
 * @package App\Http\Controllers\Api\V1
 */
class BookCommentsController extends ApiController
{
    //获取评论列表
    public function list(BookComments $bookComments, Request $request)
    {
        $query = $bookComments->newQuery();
        $modelId = $request->input('module_id', null);
        if ($modelId) {
            $query->where('module_id', $modelId);
            $query->where('status', 2);
            if ($request->has('book_id')) {
                $query->where('book_id', $request->book_id);
            }
            if ($request->has('user_id')) {
                $query->where('user_id', $request->user_id);
            }
            //排序
            switch ($request->order) {
                case 1:
                    $query->orderBy('like_num', 'desc');
                    break;//点赞数
                default:
                    $query->orderBy('created_at', 'asc');//默认按创建评论时间排序
            }
            $list = $query->with(['user', 'book'])->paginate();
            return $this->success('成功', BookCommentsResource::collection($list));
        } else {
            $this->error('参数错误');
        }
    }

    //添加评论
    public function add(BookComments $bookComments, Request $request)
    {

        if (!$request->has('module_id') ||
            !$request->has('book_id') ||
            !$request->has('contents') ||
            !$request->has('start')
        ) {
            return $this->error('参数错误');
        }
        if (!Books::find($request->book_id)) {
            return $this->error('书籍不存在');
        }
        $bookComments->module_id = $request->module_id;
        $bookComments->book_id = $request->book_id;
        $bookComments->content = $request->contents;
        $bookComments->user_id = $request->has('user_id') ? $request->user_id : $this->getUserId();
        $bookComments->pid = $request->input('pid', 0);
        $bookScore = new BookScore();
        $bookScore->user_id = $request->has('user_id') ? $request->user_id : $this->getUserId();
        $bookScore->book_id = $request->book_id;
        $bookScore->score = $request->start;
        $bookScore->module_id = $request->module_id;
        DB::beginTransaction();
        try {
            if ($bookComments->save() && $bookScore->save()) {
                DB::commit();
                return $this->success('添加评论成功', BookCommentsResource::make($bookComments));
            } else {
                DB::rollBack();
                return $this->error('添加评论失败');
            }
        } catch (\Exception $e) {
            DB::rollBack();
            return $this->error('添加评论失败');
        }
    }

    //给评论点赞
    public function getStart(BookComments $bookComments, Request $request)
    {
        if (!$request->has('comments_id') || !$request->has('start') || !$request->has('type')) {
            return $this->error('参数错误');
        }
        $query = $bookComments->newQuery();
        DB::beginTransaction();
        try {
            $userid = $request->has('user_id') ? $request->user_id : $this->getUserId();
            $query->findOrFail($request->comments_id);
            if ($request->type == 'up' && $request->start == 1) {//点赞成功
                $res_start = BookCommentsStart::updateOrCreate(['user_id' => $userid, 'comments_id' => $request->comments_id, 'type' => 1], ['status' => 1]);
                $com_num = $query->increment('like_num', 1);
            } elseif ($request->type == 'up' && $request->start == 2) {//点赞取消
                $res_start = BookCommentsStart::updateOrCreate(['user_id' => $userid, 'comments_id' => $request->comments_id, 'type' => 1], ['status' => 2]);
                $com_num = $query->decrement('like_num', 1);
            } elseif ($request->type == 'down' && $request->start == 1) {//踩成功
                $res_start = BookCommentsStart::updateOrCreate(['user_id' => $userid, 'comments_id' => $request->comments_id, 'type' => 2], ['status' => 1]);
                $com_num = $query->increment('cai_num', 1);
            } elseif ($request->type == 'down' && $request->start == 2) {//踩取消
                $res_start = BookCommentsStart::updateOrCreate(['user_id' => $userid, 'comments_id' => $request->comments_id, 'type' => 2], ['status' => 2]);
                $com_num = $query->decrement('cai_num', 1);
            }
            if ($res_start && $com_num) {
                DB::commit();
                return $this->success('操作成功');
            } else {
                DB::rollBack();
                return $this->error('操作失败');
            }

        } catch (\Exception $e) {
            DB::rollBack();
            return $this->error('评论id无效');
        }
    }
}
